DLSS.GeneralViewPort = Ext.extend(Ext.Viewport,{
    constructor:function() {
        document.body.innerHTML = '';
        DLSS.COMPONENTS_ROOT = this;
        
        Ext.AutoMask.enable({
            msg:'Loading...',
            errorMsg:'There are some errors on server<br>or in connection with server.'
        });
        Ext.QuickTips.init();
        Ext.state.Manager.setProvider(new Ext.state.CookieProvider());

        this.defaultPanel = new Ext.Panel({
            header:false,
            border:false,
            frame:false,
            region:'center',
            layout:'card',
            activeItem: 0,
            items:[
                new Ext.Panel({
                    tbar:this.getToolBarForPage(0),
                    html:""
                })
            ]
        });
        this.eventsCombo = new Ext.form.ComboBox({
        	emptyText:'Please, select an event',
        	name:'methods',
        	store:DLSS.eventsStore,
        	displayField:'title',
        	valueField:'id',
        	width:195,
        	listWidth:195,
        	mode:'local',
        	triggerAction:'all',
        	editable:false,
        });
        this.categoriesGrid = new Ext.grid.GridPanel({
           	store: DLSS.categoriesStore,
           	autoHeight:true,
           	viewConfig:{
                forceFit:true
           	},
           	columns: [
            	{header:"name",width:'100%', dataIndex: 'name', sortable: false,menuDisabled:true,resizable:false,fixed:true}
            ],
            sm:new Ext.grid.RowSelectionModel({
				singleSelect:true
			}),
           	border:false,
           	frame:false,
           	header:false,
           	hideHeaders:true
       	});
       	
        DLSS.GeneralViewPort.superclass.constructor.call(this,{
            layout:'border',
            items:[
            	new Ext.Panel({
                    region:'north',
                    frame:true,
                    cls:'head-title',
                    bodyStyle:'font-size:14px;font-weight:bold;color:#D0D0D0;font-family:Tahoma;',
                    html:'Event Manager',
                    height:30
                }), 
                this.defaultPanel,
            	new Ext.Panel({
            		region:'west',
            		id:'west-panel',
	           		title:'',
	           		split:true,
            		width: 200,
            		minSize: 175,
            		maxSize: 400,
            		collapsible: true,
            		margins:'0 0 5 5',
            		cmargins:'0 5 5 5',
            		//layout:'accordion',
            		layoutConfig:{
		                animate:true
            		},
            		items: [
            			new Ext.Panel({
            			 	border:false,
            				frame:false,
            				autoScroll:true,
            				title:'Event',
            				items:[
            					this.eventsCombo,
	           					new Ext.Panel({
	                            	border:false,
	                                header:false,
	                                width:195,
	                                frame:false,
	                                layout:'table',
	                                layoutConfig:{
	                                	columns:2
	                                },
	                                buttons: [{
											cls:'dlss-btn-icon',
											icon: 'static/images/addButton.png',
											tooltip: 'add event', 
											handler: function() {
												DLSS.eventsModal.add();
											},
											scope: this
										},{
											cls:'dlss-btn-icon',
											icon: 'static/images/editButton.png',
											tooltip: 'edit event', 
											handler: function() {
												var index = this.eventsCombo.store.find(this.eventsCombo.valueField, this.eventsCombo.getValue());
												var record;
												if (index!=-1) {
  													record = this.eventsCombo.store.getAt(index);
													DLSS.eventsModal.edit(record);
												} else {
													Ext.Msg.show({title:'Info', msg: 'Please, select an event to edit',
             											buttons: Ext.Msg.OK, scope:this, icon: Ext.Msg.WARNING});
												}
											},
											scope: this
										}
	                                ]
                                })
            				]
            			}),
            			new Ext.Panel({
				            title:'Categories',
				            frame:false,
				            width:200,
				            height:250,
				            autoScroll:true,
				            //style:'margin-bottom:10px; margin-left:20px; margin-top:30px;',
				            items:[
				            	this.categoriesGrid,
				            	new Ext.Panel({
	                            	border:false,
	                                header:false,
	                                width:195,
	                                frame:false,
	                                layout:'table',
	                                layoutConfig:{
	                                	columns:2
	                                },
	                                buttons: [{
						            	cls:'dlss-btn-icon',
											icon: 'static/images/addButton.png',
											tooltip: 'add category', 
											handler: function() {
												DLSS.categoriesModal.add();
											},
											scope: this
										},{
											cls:'dlss-btn-icon',
											icon: 'static/images/editButton.png',
											tooltip: 'edit category', 
											handler: function() {
												var record = this.categoriesGrid.getSelectionModel().getSelected();
												if(record) {
													DLSS.categoriesModal.edit(record);
												} else {
													Ext.Msg.show({title:'Info', msg: 'Please, select category to edit',
             											buttons: Ext.Msg.OK, scope:this, icon: Ext.Msg.WARNING});
             									}
											},
											scope: this
										}
									]
		                       	})
				            ]
	        			})
        			]
        		})
        	]	
        });
        this.eventsCombo.on('select', function(selection){
        	Ext.Ajax.request({url:'updateContext.htm',params:{current_event: selection.getValue()},scope:this,method:'POST'});
        	DLSS.categoriesStore.reload();
        	DLSS.couplesStore.reload();
        	DLSS.adjudicatorsStore.reload();
        	DLSS.dancesStore.reload();
        },this);
        this.categoriesGrid.getSelectionModel().on('rowselect', function(sm,row,rec){
        	Ext.Ajax.request({url:'updateContext.htm',params:{current_category: rec.get('id')},scope:this,method:'POST'});
        	DLSS.couplesStore.reload();
        	DLSS.roundsStore.reload();
        	DLSS.categAdjudicatorsStore.reload();
        	DLSS.categDancesStore.reload();
        },this);
    },
    /*checkLogin:function(response) {
        var res = Ext.util.JSON.decode(response.responseText);
        if (!res.logged) {
            new DLSS.LoginWindow();
        }
        else {
            DLSS.CURRENT_LOGGED_USER = res.login;
            DLSS.COMPONENTS_ROOT.logIn();
        }
    },*/
    checkEvent:function() {
         /*if (DLSS.CURRENT_EVENT < 0) {
            Ext.Msg.show({
         	    title:'Info',
            	msg: 'Please, select an event first',
             	buttons: Ext.Msg.OK,
             	scope:this,
             	icon: Ext.Msg.WARNING
         	});
         	return false;
         }*/
         return true;
    },
    setView:function(id) {
        //Ext.state.Manager.set(DLSS.CURRENT_VIEW_COOKIE, id)
        //Ext.state.Manager.set(DLSS.CURRENT_EVENT_COOKIE, DLSS.CURRENT_EVENT)
        this.defaultPanel.getLayout().setActiveItem(id);
    },
    logIn:function() {
        this.defaultPanel.add(new DLSS.Main());
        this.defaultPanel.add(new DLSS.Registration());
        this.defaultPanel.add(new DLSS.Results());
        this.doLayout();
		this.setView(1);
        /*this.setView(Ext.state.Manager.get(DLSS.CURRENT_VIEW_COOKIE) || DLSS.Registration);
		DLSS.CURRENT_EVENT = Ext.state.Manager.get(DLSS.CURRENT_EVENT_COOKIE);
		if (DLSS.CURRENT_EVENT >= 0) {
			this.eventsCombo.select(1);
		}*/
		
        var els = Ext.DomQuery.select('div[class=custom-user-login]');
        for (var i=0;i<els.length;i++) {
            els[i].innerHTML = DLSS.CURRENT_LOGGED_USER;
            if (!Ext.isIE) els[i].style.display = 'inline';
        }
    },
    getToolBarForPage:function(page) {
        return [
                {
                    text:'Main',
                    cls:"x-btn-text-icon",
                    //icon:'css/img/reports.png',
                    handler:function() {
                        if(this.checkEvent()) {
                        	this.setView(DLSS.MAIN);
                        }
                    },
                    scope:this
                },{
                    text:'Registration',
                    disabled:page===DLSS.REGISTRATION,
                    cls:"x-btn-text-icon",
                    //icon:'css/img/changepassword.png',
                    handler:function() {
                        if(this.checkEvent()) {
                        	this.setView(DLSS.REGISTRATION);
                       	}
                    },
                    scope:this
                },{
                    text:'Results',
                    disabled:page===DLSS.RESULTS,
                    cls:"x-btn-text-icon",
                    //icon:'css/img/changepassword.png',
                    handler:function() {
                        if(this.checkEvent()) {
                        	this.setView(DLSS.RESULTS);
                       	}
                    },
                    scope:this
                },{
                    xtype: 'tbsplit',
		   	        text: 'Reports',
                    cls:"x-btn-text-icon",
                    //icon:'css/img/admin.png',
                    scope:this,
		   	        menu: new Ext.menu.Menu({
                        items: [
                            {
                                text: 'Final Protocol',
                                //icon:'css/img/clients.png',
                                handler:function() {
                                	if(this.checkEvent()) {
                        	        	var features = "menubar=yes,location=no,resizable=yes,scrollbars=yes,status=no";
                             			window.open('finalProtocol.pdf', features);
                             		}
                             	},
                                scope:this
                            },{
                                text:'Results',
                                disabled:page===DLSS.REPORT2,
                                //icon:'css/img/user.png',
                                handler:function() {
                                    if(this.checkEvent()) {
                        				this.setView(DLSS.REPORT2);
                        			}
                                },
                                scope:this
                            }
                        ]
                    })
                },
                {xtype:'tbfill'},
                {xtype:'tbtext',text:'<div class="custom-user-login"></div>'},
                {xtype: 'tbspacer'},
                {
                    text:'Logout',
                    handler:function() {
                        Ext.Ajax.request({
                            url: 'j_acegi_logout',
                            success:function() {
                                //this.setView(DLSS.REPORTS_REGIONS);
                                //new DLSS.LoginWindow();
                            },
                            scope:this
                        });
                    },
                    scope:this
                }
            ];
        //}
    }
 
});

Ext.onReady(function(){
    new DLSS.GeneralViewPort();
    DLSS.COMPONENTS_ROOT.render();
    //DLSS.CURRENT_LOGGED_USER = res.login;
    DLSS.COMPONENTS_ROOT.logIn();
    DLSS.eventsStore.load();
    //DLSS.categoriesStore.load();
    /*Ext.Ajax.request({
        url:'login.uc',
        success:function(responce){this.checkLogin(responce);},
        scope:DLSS.COMPONENTS_ROOT
    });*/
    
});
DLSS.MAIN = 1;
DLSS.REGISTRATION = 2;
DLSS.RESULTS = 3;
DLSS.REPORTS = 4;
DLSS.REPORT1 = 5;
DLSS.REPORT2 = 6;

DLSS.CURRENT_VIEW_COOKIE = 'CURRENT_VIEW';
DLSS.CURRENT_EVENT_COOKIE = 'CURRENT_EVENT';
